package com.dao;

import com.pojo.Clazz;
import com.vo.ClazzAndStudent;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

/**
 * Created By ShaoXikai on 2022/7/14
 */
public interface ClazzDao {

    @Select("select s.s_name,s.s_sex,s.s_age,c.c_name,c.c_location from tedu.student s,tedu.clazz c where c.c_name = #{name}")
    List<ClazzAndStudent> findClazzAndStudent(String name);

    @Select("select c_name,c_location from tedu.clazz where id = #{cid}")
    @Results(id = "mapping", value = {
            @Result(id = true, column = "id", property = "id", jdbcType = JdbcType.INTEGER),
            @Result(column = "c_name", property = "cName"),
            @Result(column = "c_location", property = "cLocation")
    })
    Clazz findClazzByCid(@Param("cid") Integer id);

    @Select("select * from tedu.clazz")
    @Results(id = "oneToMany", value = {
            @Result(id = true, column = "id", property = "id", jdbcType = JdbcType.INTEGER),
            @Result(column = "c_name", property = "cName"),
            @Result(column = "c_location", property = "cLocation"),
            @Result(column = "id", property = "students",
                    many = @Many(select = "com.dao.StudentDao.findByCid",
                            fetchType = FetchType.LAZY))
    })
    List<Clazz> findAllClazz();

}
